#include "../lvgl/lvgl.h"
#include "lvgl_photo_brower.h"
#include <stdio.h>
#include <stdlib.h>
 
#define GRID_SIZE 20
#define GRID_WIDTH (LV_HOR_RES / GRID_SIZE)
#define GRID_HEIGHT (LV_VER_RES / GRID_SIZE)
 
typedef struct {
    lv_obj_t * obj;
    int16_t x;
    int16_t y;
} SnakeSegment;
 
static SnakeSegment snake[100];
static uint16_t snake_length = 3;
static int16_t dir_x = 1, dir_y = 0;
static lv_obj_t * food;
static lv_timer_t * game_timer;
static bool game_paused = false;
static uint16_t score = 0;
 
// 定义方向控制按钮为静态全局变量
static lv_obj_t * btn_up;
static lv_obj_t * btn_down;
static lv_obj_t * btn_left;
static lv_obj_t * btn_right;
static lv_obj_t * btn_pause;
static lv_obj_t * btn_exit;
static lv_obj_t * score_label;
 
static void create_food() {
    if(food) lv_obj_del(food);
    
    int16_t food_x = rand() % GRID_WIDTH;
    int16_t food_y = rand() % GRID_HEIGHT;
    
    food = lv_obj_create(lv_scr_act());
    lv_obj_set_size(food, GRID_SIZE, GRID_SIZE);
    lv_obj_set_pos(food, food_x * GRID_SIZE, food_y * GRID_SIZE);
    lv_obj_set_style_bg_color(food, lv_color_hex(0xFF0000), 0);
    lv_obj_set_style_border_width(food, 0, 0);
}
 
static void game_update(lv_timer_t * timer) {
    if (game_paused) return;
 
    // 移动蛇身
    for(int i = snake_length - 1; i > 0; i--) {
        snake[i].x = snake[i-1].x;
        snake[i].y = snake[i-1].y;
        lv_obj_set_pos(snake[i].obj, snake[i].x * GRID_SIZE, snake[i].y * GRID_SIZE);
    }
    
    // 移动蛇头
    snake[0].x += dir_x;
    snake[0].y += dir_y;
    lv_obj_set_pos(snake[0].obj, snake[0].x * GRID_SIZE, snake[0].y * GRID_SIZE);
    
    // 检查是否吃到食物
    if(snake[0].x * GRID_SIZE == lv_obj_get_x(food) && 
       snake[0].y * GRID_SIZE == lv_obj_get_y(food)) {
        // 增加蛇长度
        snake[snake_length].obj = lv_obj_create(lv_scr_act());
        lv_obj_set_size(snake[snake_length].obj, GRID_SIZE, GRID_SIZE);
        lv_obj_set_style_bg_color(snake[snake_length].obj, lv_color_hex(0x00FF00), 0);
        lv_obj_set_style_border_width(snake[snake_length].obj, 0, 0);
        snake_length++;
        score += 10;
        lv_label_set_text_fmt(score_label, "Score: %d", score);
        
        create_food();
    }
    
    // 检查碰撞
    if(snake[0].x < 0 || snake[0].x >= GRID_WIDTH || 
       snake[0].y < 0 || snake[0].y >= GRID_HEIGHT) {
        lv_timer_pause(game_timer);
        return;
    }
    
    for(int i = 1; i < snake_length; i++) {
        if(snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
            lv_timer_pause(game_timer);
            return;
        }
    }
}
 
static void direction_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * btn = lv_event_get_target(e);
        if(btn == btn_up) {
            if(dir_y == 0) { dir_x = 0; dir_y = -1; }
        } else if(btn == btn_down) {
            if(dir_y == 0) { dir_x = 0; dir_y = 1; }
        } else if(btn == btn_left) {
            if(dir_x == 0) { dir_x = -1; dir_y = 0; }
        } else if(btn == btn_right) {
            if(dir_x == 0) { dir_x = 1; dir_y = 0; }
        } else if(btn == btn_pause) {
            game_paused = !game_paused;
            if (game_paused) {
                lv_timer_pause(game_timer);
            } else {
                lv_timer_resume(game_timer);
            }
        } else if(btn == btn_exit) {
            lv_timer_pause(game_timer);
            lv_obj_del(btn_exit);
            lv_obj_del(btn_pause);
            lv_obj_del(score_label);
            for(int i = 0; i < snake_length; i++) {
                lv_obj_del(snake[i].obj);
            }
            if(food) lv_obj_del(food);
        }
    }
}

static void keypad_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_key_t * key = lv_event_get_param(e);
    
    if(code == LV_EVENT_KEY) {
        switch(*key) {
            case LV_KEY_UP:
                if(dir_y == 0) { dir_x = 0; dir_y = -1; }
                break;
            case LV_KEY_DOWN:
                if(dir_y == 0) { dir_x = 0; dir_y = 1; }
                break;
            case LV_KEY_LEFT:
                if(dir_x == 0) { dir_x = -1; dir_y = 0; }
                break;
            case LV_KEY_RIGHT:
                if(dir_x == 0) { dir_x = 1; dir_y = 0; }
                break;
            case LV_KEY_ESC:
                lv_timer_pause(game_timer);
                lv_obj_del(btn_exit);
                lv_obj_del(btn_pause);
                lv_obj_del(score_label);
                for(int i = 0; i < snake_length; i++) {
                    lv_obj_del(snake[i].obj);
                }
                if(food) lv_obj_del(food);
                break;
        }
    }
}

void snake_game_init(DI_P dip) {
    // 初始化蛇
    for(int i = 0; i < snake_length; i++) {
        snake[i].obj = lv_obj_create(lv_scr_act());
        lv_obj_set_size(snake[i].obj, GRID_SIZE, GRID_SIZE);
        lv_obj_set_pos(snake[i].obj, (GRID_WIDTH/2 - i) * GRID_SIZE, GRID_HEIGHT/2 * GRID_SIZE);
        lv_obj_set_style_bg_color(snake[i].obj, lv_color_hex(0x00FF00), 0);
        lv_obj_set_style_border_width(snake[i].obj, 0, 0);
        snake[i].x = GRID_WIDTH/2 - i;
        snake[i].y = GRID_HEIGHT/2;
    }
 
    // 创建食物
    create_food();
    
    // 创建游戏定时器
    game_timer = lv_timer_create(game_update, 200, NULL);
    
    // 调整按键大小
    #define BUTTON_SIZE (GRID_SIZE * 4)  // 调整按键大小为 GRID_SIZE 的两倍
    
    // 创建控制按钮并设置位置到右下角
    btn_down = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_down, BUTTON_SIZE, BUTTON_SIZE);
    lv_obj_set_pos(btn_down, LV_HOR_RES - BUTTON_SIZE -80, LV_VER_RES - BUTTON_SIZE);
    lv_obj_add_event_cb(btn_down, direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label_down = lv_label_create(btn_down);
    lv_label_set_text(label_down, LV_SYMBOL_DOWN);
    lv_obj_center(label_down);
    
    btn_left = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_left, BUTTON_SIZE, BUTTON_SIZE);
    lv_obj_set_pos(btn_left, LV_HOR_RES - BUTTON_SIZE * 2 -80, LV_VER_RES - BUTTON_SIZE);
    lv_obj_add_event_cb(btn_left, direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label_left = lv_label_create(btn_left);
    lv_label_set_text(label_left, LV_SYMBOL_LEFT);
    lv_obj_center(label_left);
    
    btn_right = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_right, BUTTON_SIZE, BUTTON_SIZE);
    lv_obj_set_pos(btn_right, LV_HOR_RES -80, LV_VER_RES - BUTTON_SIZE);
    lv_obj_add_event_cb(btn_right, direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label_right = lv_label_create(btn_right);
    lv_label_set_text(label_right, LV_SYMBOL_RIGHT);
    lv_obj_center(label_right);
    
    btn_up = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_up, BUTTON_SIZE, BUTTON_SIZE);
    lv_obj_set_pos(btn_up, LV_HOR_RES - BUTTON_SIZE -80, LV_VER_RES - BUTTON_SIZE * 2);
    lv_obj_add_event_cb(btn_up, direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label_up = lv_label_create(btn_up);
    lv_label_set_text(label_up, LV_SYMBOL_UP);
    lv_obj_center(label_up);
 
    // 创建暂停按钮
    btn_pause = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_pause, 50, 50);
    lv_obj_set_pos(btn_pause, 740 , 10);
    lv_obj_add_event_cb(btn_pause, direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t * label_pause = lv_label_create(btn_pause);
    lv_label_set_text(label_pause, LV_SYMBOL_PAUSE);
    lv_obj_center(label_pause);
 
    // 创建退出按钮
    btn_exit = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_exit, 50, 50);
    lv_obj_set_pos(btn_exit, 740 , 10 + 50 + 10);
    lv_obj_add_event_cb(btn_exit, exit_to_main, LV_EVENT_CLICKED, dip);
    lv_obj_t * label_exit = lv_label_create(btn_exit);
    lv_label_set_text(label_exit, LV_SYMBOL_CLOSE);
    lv_obj_center(label_exit);
 
    // 创建计分系统
    score_label = lv_label_create(lv_scr_act());
    lv_obj_set_pos(score_label, 10, 10);
    lv_label_set_text_fmt(score_label, "Score: %d", score);
 
    // 设置触摸控制
    lv_obj_add_event_cb(lv_scr_act(), direction_event_cb, LV_EVENT_CLICKED, NULL);
    lv_group_t * g = lv_group_create();
    lv_group_add_obj(g, lv_scr_act());
    lv_group_add_obj(g, btn_up);
    lv_group_add_obj(g, btn_down);
    lv_group_add_obj(g, btn_left);
    lv_group_add_obj(g, btn_right);
    lv_group_add_obj(g, btn_pause);
    lv_group_add_obj(g, btn_exit);
    lv_indev_t * indev = lv_indev_get_next(NULL);
    if(indev && lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER) {
        lv_indev_set_group(indev, g);
    } else {
        printf("No touch input device found.\n");
    }
}

// void exit_to_main(lv_event_t * e)
// {
//     // DI_P dip = ;
//     printf("*************\n");
//     lv_obj_clean(lv_scr_act());
//     if (Show_Main_Screen(((DI_P)(e->user_data)),DEFULT_DIR_PATH) == false) {
//         printf("显示目录列表失败！\n");
//         return false;
//     }
// }

void exit_to_main(lv_event_t * e)
{
    DI_P dip = (DI_P)(e->user_data);
    printf("*************\n");
    
    // 释放贪吃蛇游戏相关资源
    release_snake_game_resources();
    
    // 显示主屏幕
    if (Show_Main_Screen(dip, DEFULT_DIR_PATH) == false) {
        printf("显示目录列表失败！\n");
    }
}

// 专门释放贪吃蛇游戏资源的函数
void release_snake_game_resources(void)
{
    // 1. 删除游戏定时器
    if(game_timer) {
        lv_timer_pause(game_timer);
        lv_timer_del(game_timer);
        game_timer = NULL;
    }
        
    printf("贪吃蛇游戏资源已释放\n");
    return;
}


